package com.flashcash.solu.common.network;


import com.flashcash.solu.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ServiceGenerator {

//    //    prod
//    public static final String HARVESTER_URL = "115.159.99.132";
//    private static final String API_BASE_URL = "http://139.196.34.53:8888";
//    public static final int HARVESTER_PORT = 9696;

    //new inside contry
    public static final String HARVESTER_URL = BuildConfig.HARVESTER_IP;
    private static final String API_BASE_URL = BuildConfig.API_BASE_URL;
    public static final int HARVESTER_PORT = BuildConfig.HARVESTER_PORT;
//    Tets
//        public static final String HARVESTER_URL = "192.168.31.240";
//    public static final int HARVESTER_PORT = 8090;
//        private static final String API_BASE_URL = "http://192.168.31.240:8888";


    //    public  static final String  API_BASE_URL = "http://192.168.31.132:8888/";
    private static HttpLoggingInterceptor logging;
    private static OkHttpClient.Builder httpClientBuilder;
    private static OkHttpClient okHttpClient;
    private static Retrofit.Builder retrofitBuilder;
    private static Retrofit retrofit;

    static {
        logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        httpClientBuilder = new OkHttpClient.Builder()
                .connectTimeout(3, TimeUnit.MINUTES)
                .readTimeout(3, TimeUnit.MINUTES)
                .writeTimeout(3, TimeUnit.MINUTES)
                .addInterceptor(new DefaultHeaderAddInterceptor())
                .addInterceptor(new FirebaseHeaderInterceptor())
                .addInterceptor(logging)
//                .addInterceptor(new Handle401Interceptor())
        ;

        okHttpClient = httpClientBuilder.build();
        retrofitBuilder = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .addConverterFactory(GsonConverterFactory.create());

        retrofit = retrofitBuilder.client(okHttpClient).build();
    }

    public static <S> S createService(Class<S> serviceClass) {
        return retrofit.create(serviceClass);
    }

    public static String getApiBaseUrl(){
        return API_BASE_URL;
    }

    public static OkHttpClient getOkHttpClient(){
        return okHttpClient;
    }
}